package com.bw.zg3park2212.emp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.base.entity.EmpAddParam
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.databinding.ActivityEmpAddBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/EmpAddActivity")
class EmpAddActivity : BaseActivity<ActivityEmpAddBinding,EmpViewModoel>() {
    var depart=""
    override fun intitView() {
        binding.empaddChange.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }
        binding.empaddIv.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }
        viewmodel.depart()

    }

    override fun intitData() {
        viewmodel.departSuccse.observe(this){bean->
            val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, bean.map { it.name })
            binding.empaddDepart.adapter=arrayAdapter
            arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            binding.empaddDepart.onItemSelectedListener=object : AdapterView.OnItemSelectedListener{
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long,
                ) {
                    depart=bean.get(position).name

                }

                override fun onNothingSelected(parent: AdapterView<*>?) {

                }

            }
            binding.keep.setOnClickListener {
                val empAddParam=EmpAddParam()
                empAddParam.name=binding.empaddName.text.toString()
                empAddParam.email=binding.empaddEmail.text.toString()
                if (depart=="研发部"){
                    empAddParam.departmentId=1
                }else if (depart=="设计部"){
                    empAddParam.departmentId=2
                }else if (depart=="后勤部"){
                    empAddParam.departmentId=3
                }
                viewmodel.empadd(empAddParam)
            }
        }
        viewmodel.empaddSuccse.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            var bean=data!!.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            Glide.with(this).load(bean.path).circleCrop().into(binding.empaddIv)
        }
    }
}